﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace game
{
    public class Asteroid : RegularModelObject
    {
        protected bool rotating = false;
        public float RotationSpeed;

        public Asteroid(Scene scene, ModelUnit model, Vector3 position, int layerIndex)
            : base(scene, model, layerIndex)
        {
            this.position = position;
            rotation.Y = (float)(ShooterGame.Instance.Random.NextDouble() * 2 * Math.PI);
            RotationSpeed = ((float)ShooterGame.Instance.Random.NextDouble() - 0.5f) * 0.0002f;
            moveSpeed = RandomHelper.RandomBetween(0.0f, 1.0f) * 5.0f;
            moveDir = RandomHelper.RandomVector();
            moveDir.Y = 0.0f;

            Health = 30.0f;
            MaxHealth = Health;
            damage = 50.0f;
        }

        public void SetStatic()
        {
            RotationSpeed = 0;
            moveSpeed = 0;
        }

        public void SetSize(float size)
        {
            Scale = new Vector3(size);
            Health = size * 2.0f;
        }

        public override void Update(GameTime gameTime)
        {
            rotation.Y = SimpleMovement.Rotate(rotation.Y, RotationSpeed);
            World = Matrix.CreateScale(Scale) * Matrix.CreateRotationY(rotation.Y) * Matrix.CreateTranslation(position);
            bSphereTransformedUpToDate = false;

            position = SimpleMovement.MoveForward(position, moveSpeed, moveDir);
            //scene.ObjectMgr.CheckCollisions(this);
            
        }

        public override void DrawOpaque(GameTime gameTime)
        {
            //ShooterGame.Instance.GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
            //ShooterGame.Instance.GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;

            base.DrawOpaque(gameTime);
        }

        public override void Collision(GameObject obj)
        {
            if (obj is Asteroid)
            {
                moveSpeed = 0;
                RotationSpeed = 0;
                return;
            }
            Health -= obj.GetDamage();
            if (Health <= 0)
            {
                Destroyed();
            }
            else
            {
                //scene.ExplosionMgr.StoneSplash(obj.Position);
            }
        }

        protected override void Destroyed()
        {
            base.Destroyed();
            //scene.ExplosionMgr.FlatExplosion(position, Scale.X * 100, 4000);
            scene.ExplosionMgr.BigGreenExplosion(position, Scale.X);
            scene.ShockWave(position, Scale.X * 0.1f);
            scene.SoundMgr.Asteroid(position);
        }
    }
}
